package com.aimanager.report.config;

import org.apache.ibatis.session.SqlSessionFactory;
import org.mybatis.spring.SqlSessionFactoryBean;
import org.mybatis.spring.SqlSessionTemplate;
import org.mybatis.spring.annotation.MapperScan;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.boot.jdbc.DataSourceBuilder;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Primary;
import org.springframework.core.io.support.PathMatchingResourcePatternResolver;

import javax.sql.DataSource;

/**
 * 多数据源配置-aimanager库
 */
@Configuration
// 配置mybatis的接口类放的地方
@MapperScan(basePackages = "com.aimanager.report.mapper.aimDb", sqlSessionFactoryRef = "PrimarySqlSessionFactory")
public class DataSourceConfigAim {

    // 将这个对象放入Spring容器中
    @Bean(name = "PrimaryDataSource")
    // 表示这个数据源是默认数据源
    @Primary
    // 读取application.properties中的配置参数映射成为一个对象
    // prefix表示参数的前缀
    @ConfigurationProperties(prefix = "spring.datasource.db1")
    public DataSource getPrimaryDateSource() {
        return DataSourceBuilder.create().build();
    }


    @Bean(name = "PrimarySqlSessionFactory")
    // 表示这个数据源是默认数据源
    @Primary
    // @Qualifier表示查找Spring容器中名字为test1DataSource的对象
    public SqlSessionFactory primarySqlSessionFactory(@Qualifier("PrimaryDataSource") DataSource datasource)
            throws Exception {
        SqlSessionFactoryBean bean = new SqlSessionFactoryBean();
        bean.setDataSource(datasource);
        bean.setMapperLocations(
                // 设置mybatis的xml所在位置
                new PathMatchingResourcePatternResolver().getResources("classpath*:mapper/aimMapper/*.xml"));
        return bean.getObject();
    }
    @Bean("PrimarySqlSessionTemplate")
    // 表示这个数据源是默认数据源
    @Primary
    public SqlSessionTemplate primarySqlSessionTemplate(
            @Qualifier("PrimarySqlSessionFactory") SqlSessionFactory sessionFactory) {
        return new SqlSessionTemplate(sessionFactory);
    }
/*
spring:
  datasource:
    # aimanager 智能管理平台record日志表连接
    db1:
      driver-class-name: com.mysql.cj.jdbc.Driver
      jdbc-url: jdbc:mysql://${db1.url}:${db1.port}/aimanager?useSSL=false&useUnicode=true&characterEncoding=utf8&characterSetResults=utf8&serverTimezone=GMT
      username: ${db1.username}
      password: ${db1.password}
    # clarity 清晰度
    db2:
      driver-class-name: com.mysql.cj.jdbc.Driver
      jdbc-url: jdbc:mysql://${db2.url}:${db2.port}/clarity?useSSL=false&useUnicode=true&characterEncoding=utf8&characterSetResults=utf8&serverTimezone=GMT
      username: ${db2.username}
      password: ${db2.password}
    # image_cls 图片分类
    db3:
      driver-class-name: com.mysql.cj.jdbc.Driver
      jdbc-url: jdbc:mysql://${db3.url}:${db3.port}/image_cls?useSSL=false&useUnicode=true&characterEncoding=utf8&characterSetResults=utf8&serverTimezone=GMT
      username: ${db3.username}
      password: ${db3.password}
    # bankcard-ocr 银行卡识别
    db4:
      driver-class-name: com.mysql.cj.jdbc.Driver
      jdbc-url: jdbc:mysql://${db1.url}:${db1.port}/bankcard_ocr?useSSL=false&useUnicode=true&characterEncoding=utf8&characterSetResults=utf8&serverTimezone=GMT
      username: ${db1.username}
      password: ${db1.password}
    hikari:
      minimum-idle: 5
      # 空闲连接存活最大时间，默认600000（10分钟）
      idle-timeout: 180000
      # 连接池最大连接数，默认是10
      maximum-pool-size: 30
      # 此属性控制池中连接的最长生命周期，值0表示无限生命周期，默认1800000即30分钟
      max-lifetime: 60000
      # 数据库连接超时时间,默认30秒，即30000
      connection-timeout: 35000

mybatis:
  configuration:
    map-underscore-to-camel-case: true
    #查询超时时间设置为2小时
    default-statement-timeout: 7200

db1:
  url: 10.29.20.234
  port: 3306
  username: tplznglte
  password: SHTP#rfv0721
db2:
  url: 10.28.151.204
  port: 3389
  username: DBA
  password: 'Zhizu@123'
db3:
  url: 10.28.152.58
  port: 3396
  username: cf
  password: 123456

logging:
  level:
    com.aimanager.report.mapper: DEBUG

spring:
  redis:
    url: redis://${env.redis.pwd}@${env.redis.ip}:${env.redis.port} #武汉云redis
    jedis.pool:
      min-idle: 8
      max-idle: 16
      max-wait: 5000
      max-active: 2000
*/




}